﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using ASBCC.Workflow.K2Manager;

namespace ASBCC.Workflow.Client
{
    public class WorkflowUtility
    {
        public static void TryGetDictionaryValue(Dictionary<string, object> dictionary, string key, out string value)
        {
            value = string.Empty;
            if (dictionary == null || string.IsNullOrEmpty(key) || !dictionary.ContainsKey(key)) return;
            object objValue = dictionary[key];
            if (objValue == null) return;
            value = objValue.ToString();
        }

        public static void TryGetDictionaryValue(Dictionary<string, object> dictionary, string key, out int value)
        {
            value = 0;
            if (dictionary == null || string.IsNullOrEmpty(key) || !dictionary.ContainsKey(key)) return;
            object objValue = dictionary[key];
            if (objValue == null) return;
            value = (int)objValue;
        }

        public static string GetActionType(Button actionButton)
        {
            return actionButton.Attributes["actionType"];
        }

        public static bool IsWorkflowRelated(Button actionButton)
        {
            return actionButton.Attributes["workflowRelated"] == bool.TrueString;
        }

        public static void ParseUrl(string url, out string rootUrl, out Dictionary<string, string> queryStringParameters)
        {
            rootUrl = string.Empty;
            queryStringParameters = new Dictionary<string,string>();
            int indexOfQuestionMark = url.IndexOf('?');
            if (indexOfQuestionMark >= 0)
            {
                rootUrl = url.Substring(0, indexOfQuestionMark);
                string queryString = url.Substring(indexOfQuestionMark + 1);
                if (!string.IsNullOrEmpty(queryString))
                {
                    NameValueCollection nameValueCollection = HttpUtility.ParseQueryString(queryString);
                    foreach (string key in nameValueCollection.Keys)
                    {
                        if (!string.IsNullOrEmpty(key))
                        {
                            queryStringParameters.Add(key, nameValueCollection[key]);
                        }
                    }
                }
            }
            else
            {
                rootUrl = url;
            }
        }

        public static string CombineUrl(string rootUrl, Dictionary<string, string> queryStringParameters)
        {
            if (string.IsNullOrEmpty(rootUrl)) return string.Empty;
            if (queryStringParameters == null || queryStringParameters.Count == 0) return rootUrl;
            List<string> keyValueList = new List<string>();
            foreach (KeyValuePair<string, string> parameter in queryStringParameters)
            {
                keyValueList.Add(string.Join("=", new string[] { HttpUtility.UrlEncode(parameter.Key), HttpUtility.UrlEncode(parameter.Value) }));
            }
            
            string queryString = string.Join("&", keyValueList.ToArray());
            return rootUrl + "?" + queryString;
        }

        public static string FindKeyInDictionary(Dictionary<string, string> dictionary, string key, bool ignoreCase)
        {
            if (dictionary == null) return null;
            if (ignoreCase)
            {
                foreach (KeyValuePair<string, string> keyValue in dictionary)
                {
                    if (string.Compare(keyValue.Key, key, true) == 0) return keyValue.Key;
                }
                return null;
            }
            else
            {
                if (dictionary.ContainsKey(key))
                {
                    return key;
                }
                else
                {
                    return null;
                }
            }
        }

        public static bool K2UserIsNull(K2User user)
        {
            return user == null || user.UserId <= 0;
        }

        public static void AppendReaders(IAppForm appForm, List<K2User> users)
        {
            if (appForm == null || users == null) return;
            string readers = appForm.Readers;
            foreach (K2User user in users)
            {
                string item = string.Format(";{0};", user.UserId);
                if (string.IsNullOrEmpty(readers))
                {
                    readers = item;
                }
                else
                {
                    if (!readers.Contains(item)) readers += user.UserId.ToString() + ";";
                }
            }
            appForm.Readers = readers;
        }
    }
}
